import React from 'react';
import addresx from "../../assets/css/order/addresx.module.css";
import {Modal, Toast} from "antd-mobile";
import {Link} from "react-router-dom";

export default class AddresxComponent extends React.Component{
    constructor(){
        super();
        this.state={
            uid:localStorage['uid'],
            siteList:[]
        }
    }
    componentWillMount() {
        this.getSite()
    }
    getSite(){
        const parmas=new URLSearchParams();
        let uid=this.state.uid;
        fetch('http://vueshop.glbuys.com/api/user/address/index?uid='+uid+'&token=1ec949a15fb709370f',{
            method:'POST',
            body:parmas
        }).then((res)=>res.json())
            .then(res=>{
                if(res.code===200){
                    this.setState({siteList:res.data})
                }else {
                    console.log(res.data)
                }
            })
    }
    goAdd(index){
        let aid=this.state.siteList[index].aid;
        localStorage['aid']=aid;
        this.replacePage('/user/mod?aid='+aid);
    }
    delete(index){
        const alert = Modal.alert;
        alert('', '确认要删除吗', [
            { text: '取消', onPress: () => console.log('cancel') },
            { text: '确认', onPress: () => {
                    let uid=this.state.uid;
                    let aid=this.state.siteList[index].aid;
                    const parmas=new URLSearchParams();
                    fetch('http://vueshop.glbuys.com/api/user/address/del?uid='+uid+'&aid='+aid+'&token=1ec949a15fb709370f',{
                        method:'POST',
                        body:parmas
                    }).then((res)=>res.json())
                        .then(res=>{
                            console.log(res);
                            if(res.code===200){
                                Toast.success(res.data, 1.5);
                                localStorage.removeItem('ress')
                                this.getSite();
                                this.props.history.go();
                            }
                        })
                }},
        ])
    }
    goBack(){
        this.props.history.goBack()
    }
    replacePage(path){
        this.props.history.replace(path)
    }
    btn(index){
        localStorage['ress']=index;
        localStorage['aid']=this.state.siteList[index].aid;
        this.replacePage('/order/index');
    }
    render() {
        return(
            <div className={addresx.body}>
                <div className={addresx.header}>
                    <img onClick={this.goBack.bind(this)} src={require('../../assets/images/home/goods/back.png')} alt=""/>
                    <p>选择收货地址</p>
                </div>
                <div className={addresx.head}>
                    <p>配送地址</p>
                    <Link to="/user/add" style={{color:'black'}}><span>+添加收货地址</span></Link>
                </div>
                {this.state.siteList.map((item,index)=>{
                    return(
                        <div key={index} className={addresx.cont}>
                            <div onClick={e=>{this.btn(index)}}>
                                <p><span>{item.name}</span>{item.cellphone}</p>
                                <p><span className={item.isdefault === '1'?addresx.show:addresx.hide}>[默认]</span>{item.province}{item.city}{item.area}{item.address}</p>
                            </div>
                            <img onClick={e=>{this.goAdd(index)}} className={addresx.edit} src={require('../../assets/images/home/cart/edit.png')} alt=""/>
                            <img onClick={e=>{this.delete(index)}} className={addresx.del} src={require('../../assets/images/home/cart/del.png')} alt=""/>
                        </div>
                    )
                })}
            </div>
        )
    }
}